var express = require("express");
var router = express.Router();
var pool = require("../pool");
const uuid = require('uuid')
//配置multer中间件
const multer = require('multer')
obj = multer.diskStorage({
  destination: function (req, file, cb) { //指定目录
    cb(null, 'upload')
  },
  filename: function (req, file, cb) { // 指定文件名
    // console.log(uuid.v1())
    // console.log(uuid.v4())
    let name = file.originalname
    // name:  abcd.jpg    xxxdfdd.zip
    let ext = name.substr(name.lastIndexOf('.'))
    cb(null, uuid.v4() + ext)


  }
})
const uploadTools = multer({
  storage: obj
})
// 头像上传并且录入数据
router.post('/upload',
 uploadTools.array('uploadFile'),(req,res)=>{
    //  console.log(req.files[0].filename)
    //  console.log(req.files)
    var {uname,phone,sex,uid}=req.body;
    if(req.files[0]){
   var icon='http://127.0.0.1:3000/'+req.files[0].filename;
    var sql="update at_user set uname=? , sex=? , phone=? ,icon=? where uid=?";
    pool.query(sql,[uname,sex,phone,icon,uid],(err,result)=>{
      res.send({msg:`ok`});
  
    //   console.log(icon,uid,uname,phone,sex)
    })
    }else{
         var sql1="update at_user set uname=? , sex=? , phone=? where uid=?";
    pool.query(sql1,[uname,sex,phone,uid],(err,result)=>{
      res.send({msg:`ok`});
  
    //   console.log(icon,uid,uname,phone,sex)
    })
    }
 
    
 }
)
// 购物车小圆点count数接口
router.get('/count',(req,res)=>{
  var {uid}=req.query;
  var sql='select * from at_cart where cuid=?'
  pool.query(sql,[uid],(err,result)=>{
    if(result.length>0){
          var count=0;
          for(var p of result){
              count +=p.count;
          }
          // console.log(result,count)
          res.write(JSON.stringify({
                uid: uid,
                count:count
            }));
    }else{
       res.write(JSON.stringify({
                uid: uid,
                count:0
            }));
    }
    res.end();
              
  })
})
// 用户登录、、、、、、、、、、、、、、、、、、、、、
router.post("/login", (req, res) => {
    var {
        uname,
        upwd
    } = req.body;
      // console.log(uname,upwd)
    var sql = "select * from at_user where uname=? and  upwd=? ";
    pool.query(sql, [uname, upwd], (err, result) => {
        err && console.log(err);
        if (result.length > 0) {
         
          // var count=0;
          // for(var p of result){
          //     count +=p.count;
          // }
            res.write(JSON.stringify({
                ok: 1,
                uname: result[0].uname,
                uid: result[0].uid,
                // count:count
              
            }));
           
        } else {
            res.write(JSON.stringify({
                ok: 0,
                msg: "用户名或密码错误!"
            }));
        }
        res.end();
    })
})
// 用户注册、、、、、、、、、、、、、、、、、、
router.post("/register", (req, res) => {
    var {
        uname,
        upwd
    } = req.body;
  
    var sql1 = "select * from at_user where uname=?";
    pool.query(sql1, [uname], (err, result) => {
        if (err) {
            console.log(err);
            res.write(JSON.stringify({
                ok: 0,
                msg: "注册失败"
            }))
            res.end();
            
        } else if (result.length > 0) {
            res.write(JSON.stringify({
                ok: 0,
                msg: "用户名已存在"
               
            }))
            //  console.log(result)
            res.end();
        } else {
            var sql2 = "INSERT INTO `at_user`(`uname`, `upwd`) VALUES (?,?)";
            pool.query(sql2, [uname, upwd], (err, result) => {
                if (err) {
                    console.log(err);
                    res.write(JSON.stringify({
                        ok: 0,
                        msg: "注册失败"
                    }))
                } else {
                    res.write(JSON.stringify({
                        ok: 1,
                        uid: result.insertId
                    }))
                }
                res.end()
            })
        }
    })
})
// 修改密码
router.post("/changepwd",(req,res)=>{
  var {uid, oldupwd, upwd}=req.body;
  var sql1="select * from at_user where uid=? and upwd=? ";
  pool.query(sql1,[uid,oldupwd],(err,result)=>{
    if(err){
      console.log(err);
      res.write(JSON.stringify({
        ok:0, msg:"密码修改失败！"
      }))
      res.end();
    }else if(result.length==0){
      res.write(JSON.stringify({
        ok:0, msg:"原密码不正确！"
      }))
      res.end();
    }else{
      var sql2="UPDATE `at_user` SET `upwd`=? WHERE `uid`=?";
      pool.query(sql2,[upwd,uid],(err,result)=>{
        err&&console.log(err);
        if(err){
          console.log(err);
          res.write(JSON.stringify({
            ok:0, msg:"修改密码失败"
          }))
        }else{
          res.write(JSON.stringify({
            ok:1, msg:"密码修改成功"
          }))
        }
        res.end();
      })
    }
  })
})



//获取用户信息
router.get("/user", (req, res) => {
     var {uid}=req.query;
    var sql = "SELECT * FROM `at_user` WHERE `uid`=?";
    pool.query(sql, [uid], (err, result) => {
        
        res.write(JSON.stringify(result));
  
        res.end();

    })

})

module.exports = router;